/**
 * Created by su on 2018/6/13.
 */
$(function () {
    var head = {
        'Version': '1.0',
        'CertType': 'post',
        'Certification': 'sign',
        'UserId': 'N6868',
        'DeviceToken': '',
        'Plat': '2',
        'OSInformation': 'mac ios',
        'Channel': '10.0',
        "ICECode": 3
    };
    var request = {
        'RGuid': 'gateway.cryptonym.item',
        'Pri': 'xxsh.javascript.www',
        'Content': {}
    };
    var Url = "../php/do_gateway.php";

    function base64(json) {
        var rv = JSON.stringify(json);
        rv = encodeURIComponent(rv);
        rv = unescape(rv);
        rv = window.btoa(rv);
        return rv;
    }

    function post($data, RGuidUrl, successCallback, errorCallback) {
        request.Content = base64($data);
        request.RGuid = RGuidUrl;
        var $head = base64(head);
        var $request = base64(request);
        $.ajax({
            type: 'POST',
            url: Url,
            data: {
                Head: $head,
                Request: $request
            },
            success: successCallback,
            error: errorCallback || null
        })
    };

    function getUserId() {
        request.Content = base64(request.Content);
        var $request = base64(request);
        var $head = base64(head);
        $.ajax({
            type: 'POST',
            url: Url,
            data: {
                Head: $head,
                Request: $request
            },
            success: function(request) {
                request = JSON.parse(request);
                if (request.status == "200") {
                    var userId = request.result.user_id;
                    var sign = request.result.sign;
                    head.UserId = userId;
                    head.Certification = sign;
                }

            }
        });
    }
    getUserId();

    //地区
    var area2 = new LArea();
    area2.init({
        'trigger': '#selectProvince',
        'valueTo': '#selectProvinceValue',
        'keys': {
            id: 'value',
            name: 'text'
        },
        'type': 2,
        'data': [provs_data, citys_data, dists_data]
    });
    var width = $(window).width();

    var verifyCode = new GVerify("v_container");//动态验证码


    $('#submitApply').on('click', function() {
        var addressAll =  $("#selectProvince").val();
        var addressAllValue =  $("#selectProvinceValue").val();
        var addressList = addressAll.split(",") ;
        var addressListValue = addressAllValue.split(",") ;
        var province_id = addressListValue[0];
        var province_name = addressList[0];
        var city_id = addressListValue[1];
        var city_name = addressList[1];
        var county_id = addressListValue[2];
        var county_name = addressList[2];

        var res = verifyCode.validate(document.getElementById("code_input").value);
        if(!res){
            alert("验证码错误,请重新输入！");
            return false;
        }

        var applyName = $('#applyName').val();
        var applyPhone = $('#applyPhone').val();
        var applyAddressDetail = $('#applyAddressDetail').val();
        var applyAddress = $('#applyAddress').val();
        if (applyName == "") {
            alert("姓名不能为空");
        } else if (applyPhone == "") {
            alert("联系方式不能为空");
        } else if (applyAddressDetail == "") {
            alert("详细地址不能为空");
        } else if (applyAddress == "") {
            alert("小区地址不能为空");
        }  else {
            var $data = {
                "applicant": applyName,
                "province_id": province_id,
                "province_name": province_name,
                "city_id": city_id,
                "city_name": "city_name",
                "county_id": county_id,
                "county_name": county_name,
                "phone": applyPhone,
                "address": applyAddressDetail,
                "regional_name" : applyAddress
            };
            var $url = "boxs.web.applicant.addApplicantBox";
            post(
                $data,
                $url,
                function(request) {
                    request = JSON.parse(request);
                    if (request.status == "200") {
                        alert("申请成功");
                    } else {
                        alert(request.msg);
                    }
                },
                function() {}
            )
        }
    })
})
